package com.efamily.ewallet.server;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.efamily.ewallet.client.account.bean.AccountBean;
import com.efamily.ewallet.client.profit.bean.ProfitBean;
import com.efamily.ewallet.client.profit.bean.ProfitSearchConfig;
import com.efamily.ewallet.client.profit.bean.ProfitTypeBean;
import com.efamily.ewallet.client.profit.service.ProfitService;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class ProfitServiceImpl extends RemoteServiceServlet implements
		ProfitService {
	

	public PagingLoadResult<ProfitBean> getProfits(AccountBean accountBean, 
			ProfitSearchConfig searchConfig, PagingLoadConfig config) {
		List<ProfitBean> beans = loadProfitsBeans();
		if (config.getSortInfo().getSortField() != null) {
			final String sortField = config.getSortInfo().getSortField();
			if (sortField != null) {
				Collections.sort(beans, config.getSortInfo().getSortDir()
						.comparator(new Comparator<ProfitBean>() {
							@SuppressWarnings("deprecation")
							public int compare(ProfitBean o1, ProfitBean o2) {
								if (sortField.equals("id")) {
									return o1.getId() < o2.getId() ? -1 : (o1
											.getId() == o2.getId()) ? 0 : 1;
								} else if (sortField.equals("type")) {
									return o1.getType().getName().compareTo(
											o2.getType().getName());
								} else if (sortField.equals("profit_date")) {
									return o1.getProfitDate().compareTo(
											o2.getProfitDate());
								} else if (sortField.equals("week_day")) {
									return o1.getProfitDate().getDay() < o2
											.getProfitDate().getDay() ? -1
											: (o1.getProfitDate().getDay() == o2
													.getProfitDate().getDay()) ? 0
													: 1;
								} else if (sortField.equals("month_day")) {
									return o1.getProfitDate().getDate() < o2
											.getProfitDate().getDate() ? -1
											: (o1.getProfitDate().getDate() == o2
													.getProfitDate().getDate()) ? 0
													: 1;
								} else if (sortField.equals("month")) {
									return o1.getProfitDate().getMonth() < o2
											.getProfitDate().getMonth() ? -1
											: (o1.getProfitDate().getMonth() == o2
													.getProfitDate().getMonth()) ? 0
													: 1;
								} else if (sortField.equals("year")) {
									int o1Year = o1.getProfitDate().getYear() + 1900;
									int o2Year = o2.getProfitDate().getYear() + 1900;
									return o1Year < o2Year ? -1
											: (o1Year == o2Year) ? 0 : 1;
								} else if (sortField.equals("value")) {
									return o1.getValue() < o2.getValue() ? -1
											: (o1.getValue() == o2.getValue()) ? 0
													: 1;
								}
								return 0;
							}
						}));
			}
		}

		List<ProfitBean> sublist = new ArrayList<ProfitBean>();
		int start = config.getOffset();
		int limit = beans.size();
		if (config.getLimit() > 0) {
			limit = Math.min(start + config.getLimit(), limit);
		}
		for (int i = config.getOffset(); i < limit; i++) {
			sublist.add(beans.get(i));
		}
		return new BasePagingLoadResult<ProfitBean>(sublist,
				config.getOffset(), beans.size());

	}

	public List<ProfitTypeBean> getProfitTypes(AccountBean accountBean) {
		List<ProfitTypeBean> types = loadTypeBean();
		return types;
	}

	private List<ProfitBean> loadProfitsBeans() {
		List<ProfitBean> beans = new ArrayList<ProfitBean>();
		
		ProfitBean bean1 = new ProfitBean(1, new ProfitTypeBean(1, "salary"),
				new Date(), 100, "just salary");
		beans.add(bean1);
		ProfitBean bean2 = new ProfitBean(2, new ProfitTypeBean(2, "dividend"),
				new Date(), 100, "forex");
		beans.add(bean2);
		ProfitBean bean3 = new ProfitBean(3, new ProfitTypeBean(3, "bonus"),
				new Date(), 100, "annual bonus");
		beans.add(bean3);

		return beans;
	}

	private List<ProfitTypeBean> loadTypeBean() {
		List<ProfitTypeBean> types = new ArrayList<ProfitTypeBean>();
		types.add(new ProfitTypeBean(1, "salary"));
		types.add(new ProfitTypeBean(2, "dividents"));
		types.add(new ProfitTypeBean(3, "bonus"));
		return types;
	}

	public boolean addEditProfit(AccountBean accountBean, ProfitBean profit) {
		/*
		 * if(profit.getId()==-1){ //TODO save new profit }else{
		 * ExpenseCategoryBean categoryBean = expense.getCategory();
		 * if(categoryBean.getId()==-1){
		 * categoryBean.getTypes().add(expense.getType()); //TODO save new
		 * category int id = 1;//TODO get id categoryBean.setId(id);
		 * 
		 * } //TODO update expense }
		 */

		return true;
	}

	public boolean deleteProfits(AccountBean accountBean, List<Integer> profitsID) {
		return true;
	}

}
